<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="${res}/js/jquery/jquery.js"></script>
<script type="text/javascript" src="${res}/js/jquery/jquery.form.js"></script>
<script type="text/javascript" src="${res}/js/proposal/irisPrpPrepare.js"></script>
<script type="text/javascript" src="${res_app}/js/app/proposal/irisPrpPrepare.js"></script>
<script type="text/javascript" src="${res}/js/validate/irisvalidator.js"></script>
<title>
	<c:if test="${method_Get=='add'}">新增</c:if>
	<c:if test="${method_Get=='edit'}">编辑</c:if>
	限项
</title>
<%@ include file="/common/meta.jsp"%>
</head>
<body>
	<form id="mainForm" method="post" action="${ctx}/prpapprove/saveOrgLimit">
		<table width="100%" border="0" cellpadding="0" cellspacing="1" class="t_css" id="basic">
			<tr class="li_1">
				<td align="right" width="160">
					<span class="color_red">*</span>
					项目类别：
				</td>
				<td align="left">
					<c:set var="grantType">
		                {'id':'${grantCode}',
		                'name':'${grantName}'
		                }                   
		            </c:set>
		            <!-- grant_codes_org_limt -->
					<cpt:tree id="grantType" key="grant_codes_org_limt" cacheable="false" className="hn_iuput_kuang required selectWidth3"
						readonly="true" value="${grantType}" label="项目类别" showPattern="all" shieldPattern="0" onClick="selNode" checkable="true">
					</cpt:tree>
					<input type="hidden" id="grantType_value" class="input_css" name="id.grantCode" value="${grantCode}">
					<input type="hidden" id="grantType_name" name="grantName" value="${grantName}">
				</td>
				<td align="right" width="160">
					<span class="color_red">*</span>
					业务年度：
				</td>
				<td align="left">
					<cpt:select id="statYear" key="statYear" onChange="initControlValue(this.id);" name="id.statYear"
						className="required" label="业务年度" value="${id.statYear}"></cpt:select>
				</td>
			</tr>
			<tr class="li_2">
				<td align="right" width="160">
					<span class="color_red">*</span>
					申报单位：
				</td>
				<td align="left">
					<%-- <c:set var="recomend_org">
	                {'id':'${id.orgCode}',
	                'name':'${orgName}'
	                }                   
                    </c:set>
					<cpt:tree id="recomend_org" key="recommended_org" cacheable="false" topCheckbox="false"
						className="hn_iuput_kuang required selectWidth3" readonly="true" value="${recomend_org}" label="申报单位"
						showPattern="all" shieldPattern="0" onClick="initTreeValueExt">
					</cpt:tree>
					<input type="hidden" id="recomend_org_value" name="id.orgCode" value="${id.orgCode}">
					<input type="hidden" id="recomend_org_name" name="orgName" value="${orgName}"> --%>
					<input id="orgName" type="text" label="申报单位" name="orgName"  readonly="readonly" class="input_text"   style="float:left;" value=""/>
                    <input type="hidden" class="required" label="申报单位" name="id.orgCode" id="orgCode" value="" /> 
				    <a class="inputSelectButton" title="点击置空" href="###" onclick="clearPsnFromSys();">置空</a>                  
					<img id="selectPsn" src="${res}/images/application_form_add.png" style="cursor:pointer; vertical-align:bottom;float:left " onclick='addPsnFromSys();' />
				</td>
				<td align="right" width="160">
					<span class="color_red">*</span>
					申报批次：
				</td>
				<td align="left">
					<cpt:select id="batch" name="id.batch" key="batch" label="申报批次" value="${id.batch}" className="required"></cpt:select>
				</td>
			</tr>
			<tr class="li_1">
				<td align="right" width="160">
					<span class="color_red">*</span>
					设置上限：
				</td>
				<td align="left">
					<input type="text" class="input_css required valueGENum:limitNum:1" name="limitNum" id="limitNum" label="设置上限"
						value="${limitNum}" onfocus="InitInput.setNumber(this,11,0,0);" style="text-align:right;"/>
				</td>
				<td align="right" width="160"></td>
				<td align="left"></td>
			</tr>
		</table>
		</table>
	</form>
	<table width="100%" align="center">
		<tr>
			<td>
				<input class="button_01" type="button" value="提交" onclick="save();" id="save">
				<input class="button_01" type="button" value="返回" onclick="parent.tb_remove2();">
			</td>
		</tr>
	</table>
</body>
</html>
<script type="text/javascript">
	var prcWin = new progressWin("${res}"); //初始化遮罩层
	function save() {
		validate($("#basic"), "限项");
		if(valResult() && validateOrgLimit()) {
		    irisValaddMsg("已存在类似限制，请返回列表修改");
            irisValShowMsg($("#save"));
	          return false;
		}
		if (!valResult()) {
			irisValShowMsg($("#save"));
			return false;
		}
		
		prcWin.openWin();//打开遮罩层
		$(":button,:submit").each(function() {//锁住按钮
			$(this).attr("disabled", "true");
		});
		var options = {
			success : function(msg) {
				prcWin.closeWin();
				if (msg != null && msg == "success") {//保存成功
					scmSuccess("保存成功", "100px");
					//parent.irisGrid2.getGridDom().jqGrid("refresh",0);
					parent.irisGrid.getGridDom().jqGrid("refresh", 0);
					window.setTimeout("parent.tb_remove();", 500);
				} else {
					scmError("保存失败", "100px");
					$(":button,:submit").each(function() { //打开按钮
						$(this).removeAttr("disabled");
					});
				}

			},// 表单成功提交后的回调函数
			error : function() {
				prcWin.closeWin();
				scmError("保存失败", "100px");
				$(":button,:submit").each(function() {//打开按钮
					$(this).removeAttr("disabled");
				});
			},
			dateType : 'text',
			type : "post" // 提交方式 post,get
		};

		$("#mainForm").ajaxSubmit(options);
	}

	function setValueById(name, obj, value) {
		var obj = $(obj);
		var nameId = name + "_name";
		var valueId = name + "_value";
		$("#" + nameId).val(obj.attr("name"));
		$("#" + valueId).val(obj.attr("id"));
	}

	function initTreeValueExt(treeId, treeNode) {
		if ((treeNode && treeNode.isParent)) {
			scmWarn("请选择具体的" + $("#" + treeId).attr("label"));
			
			$("#" + treeId).val("");
			$("#" + treeId).parents("td")
					.find(
							"[name$='" + treeId + "_code'],[id$='" + treeId
									+ "_code']").val("");
			$("#" + treeId).parents("td").find(
					"[name$='" + treeId + "_value'],[id$='" + treeId
							+ "_value']").val("");
			$("#" + treeId).parents("td")
					.find(
							"[name$='" + treeId + "_name'],[id$='" + treeId
									+ "_name']").val("");
			return false;
		}

		var str = treeNode.name;
		var obj = treeNode.parentNode;
		while (obj != null) {
			str = obj.name + "-" + str;
			obj = obj.parentNode;
		}

		//     initTitle(treeId);
		$("#" + treeId).parents("td").find(
				"[name$='" + treeId + "_code'],[id$='" + treeId + "_code']")
				.val(treeNode.id);
		$("#" + treeId).parents("td").find(
				"[name$='" + treeId + "_value'],[id$='" + treeId + "_value']")
				.val(treeNode.id);
		$("#" + treeId).parents("td").find(
				"[name$='" + treeId + "_name'],[id$='" + treeId + "_name']")
				.val(str);
	}
	
	
	function initTreeValueExt2(treeId, treeNode) {
		var str = treeNode.name;
		var obj = treeNode.parentNode;
		while (obj != null) {
			str = obj.name + "-" + str;
			obj = obj.parentNode;
		}
		
		$("#" + treeId).parents("td").find("[name$='" + treeId + "_code'],[id$='" + treeId + "_code']").val(treeNode.id);
		$("#" + treeId).parents("td").find("[name$='" + treeId + "_value'],[id$='" + treeId + "_value']").val(treeNode.id);
		$("#" + treeId).parents("td").find("[name$='" + treeId + "_name'],[id$='" + treeId + "_name']").val(str);
		$("#grantType").val(str);
		$("#grantType_hideName").val(str);
	}
	
	
	// 根据参数中的显示和屏蔽模式值设置显示结果
	function buildName(treeNode, params) {
		if (null != treeNode && null != params) {
			var name = treeNode.name;
			var names = name;
			var flag = true;
			if ($.trim(params.showPattern) == "all") { // 显示所有节点
				flag = false;
				var parent = treeNode.parentNode;
				var lev = 0;
				while (parent != null) {
					lev++;
					names = parent.name + "," + names;
					parent = parent.parentNode;
				}
				names = names.split(",");
				var shield = $.trim(params.shieldPattern);
				var shields = shield.split(",");
				for (var i = shields.length - 1; i >= 1; i--) { // 过滤重复数据
					if (shields[i - 1] == shields[i]) {
						shields.splice(i, 1);
					}
				}
				shields.sort(); // 对数组排序
				if (shields[0] > -1) { // 屏蔽节点
					for (var i = shields.length - 1; i >= 0; i--) {
						if (shields[i] > lev) {
							continue;
						}
						if (shields[i] != "")
							names.splice(shields[i], 1);
					}
				}
			}
			//name = "";
			if (flag) {
				// names += ",";
				// names = names.split(",");
				name = names;
			} else {
				if(!!!names){
					for ( var n in names) {
						name += names[n];
					}
				}
			}
			return name;
		}
		return null;
	}
	
	   function selNode(id,node){
	        
//	      selChildNode(node,node.checked==true);
	        var inputNameVal = "";
	        var hideIdVal = "";
	        var checkedNodes = zTreeObj.getCheckedNodes();
	        // 将所有的值按照从小到大排序
	        checkedNodes.sort(function(num1, num2) {
	                return num1.id - num2.id;
	            });
	        for ( var i = 0; i < checkedNodes.length; i++) {
	            if(!checkedNodes[i].isParent){
	                if (inputNameVal == '') {
	                    inputNameVal = checkedNodes[i].name;
	                    hideIdVal = checkedNodes[i].id;
	                } else {
	                    inputNameVal += "," + checkedNodes[i].name;
	                    hideIdVal += "," + checkedNodes[i].id;
	                }
	            }
	        }
	        
	        $("#grantType_value").val(hideIdVal);
	        $("#grantType_name").val(inputNameVal);
	        
	        /* if ($("#" + inputId + "_text").length == 0) {
	            $("#" + inputId).val(inputNameVal);
	            $("#" + hiddenId).val(hideIdVal);
	            $("#" + hiddenName).val(hiddenName);
	        } else {
	            $("#" + params.key).contents().find("#" + inputId + "_text")
	                    .val(inputNameVal);
	            $("#" + params.key).contents().find("#" + inputId).val(
	                    hideIdVal);
	        } */
	    }   
	
	function addPsnFromSys() {
// 		window.open(ctx+"/reg-organization/retrievalOrgInfo?flag=init","","top=100,left=100,width=300,height=200");
//         window.top.tb_show("检索单位",ctx+"/reg-organization/retrievalOrgInfo?flag=init&TB_iframe=true&height=500&width=980",false);
        tb_show("检索单位",ctx+"/reg-organization/retrievalOrgInfo?flag=init&TB_iframe=true&height=500&width=1300",false);
	}
	
	function selectOrgCall(code, name) {
        $("#orgName").val(name).keyup();
        $("#orgCode").val(code);
        
        tb_remove2();
    }
	function clearPsnFromSys() {
		 $("#orgName").val("");
         $("#orgCode").val("");
	}
	
	/**
	* 判断该单位+年度+批次 是否已经设了已选类别限项
	*/
	function validateOrgLimit() {
		var orgCode = $('#orgCode').val();
		var batch = $('#batch').val();
		var statYear = $('#statYear').val();
		var grantCodes = $('#grantType_value').val();
		var isExists = true;
		$.ajax({
            type: "POST",
            async:false,
            url: "${ctx}/prpapprove/validateOrgLimit",
            data: {"orgCode":orgCode,"batch":batch,"statYear":statYear,"grantCodes":grantCodes},
            beforeSend:function(XMLHttpRequest) {
//                prcWin.openWin();//打开遮罩层
            },
            success: function(msg){
                var obj = eval("(" + msg + ")");
                if(obj.isSuccess == 1) {
                	isExists = obj.isExists == 1;
                } else {
                    scmError("校验单位限项是否存在该业务类别失败");
                }
                
            },// 表单成功提交后的回调函数
            error : function() {
                scmError("操作失败,请联系系统管理员");
                $("input[type='button'][id!='back']").attr("disabled",false);
//                 prcWin.closeWin();
            }
         });
		return isExists;
	}
</script>